package com.twm.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Optional;

/**
 * @ClassName CookieUtil
 * @Description TODO
 * @Author twm17
 * @Date 2020/11/11 16:43
 * @Version 1.0
 */
public class CookieUtil {


    public static boolean existsCookie(HttpServletRequest request,String val) {

        Optional<Cookie> ck = Arrays.stream(request.getCookies())
                .filter(cookie -> cookie.getName().equals(val)).findAny();
        if(ck.isPresent()) {
            return true;
        }
        return false;
    }

    public static String getCookieValue(HttpServletRequest request,String val) {

        Optional<Cookie> ck = Arrays.stream(request.getCookies())
                .filter(cookie -> cookie.getName().equals(val)).findAny();
        if(ck.isPresent()) {
            return ck.get().getValue();
        }
        return null;
    }

}
